<?php
namespace app\restaurant\controller;

use think\Controller;
use think\facade\Request;
use wxpayfood\PayFood;

class Pay extends Controller
{
    private $appid = 'wx3d0bed1c9f6461ba';
    private $secret = '2bfb984e2458cbf90f50790dac947aa0';
    public function getOpenidByCode()
    {
        $code = $this->request->get("code");
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $this->appid . "&secret=" . $this->secret . "&js_code=" . $code . "&grant_type=authorization_code";
        $res = json_decode(file_get_contents($url), true);
        if (array_key_exists("openid", $res)) {
            return json_encode($res['openid']);
        }
    }
    public function getPayInfo()
    {
        $data = [];
        $data['money'] = $this->request->get("money") * 100;
        $data['openid'] = $this->request->get("openid");
        $payfood = new PayFood();
        $order = $payfood->getOrder($data);
        if ($order) {
            return $payfood->GetJsApiParameters($order);
        }
    }
    public function payInfo()
    {
        $xml = file_get_contents("php://input");
        $arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        if (array_key_exists('openid', $arr)) {
            file_put_contents('/www/wwwroot/cs.rongbaiwei.com/public/upload/paytest.txt', var_export($arr, true) . '\n', FILE_APPEND);
            echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
        }
    }
}
